package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.GetMsgBean;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.MyAdapter;

/**
 * Created by chenchangxing on 15/12/23.
 */
public class SystemMsgMainAdapter extends MyAdapter<GetMsgBean.DataBean> {

    public SystemMsgMainAdapter(Context context, List<GetMsgBean.DataBean> list) {
        super(context, list);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_system_msg_main, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.msgTitle.setText(list.get(position).getTitle());
        holder.msgContent.setText(list.get(position).getContent());
        holder.msgClient.setText(list.get(position).getHotline());
        String pic = list.get(position).getPhoto();
        if (pic == null || pic.isEmpty()) {
            holder.msgImg.setVisibility(View.GONE);
        } else {
            holder.msgImg.setVisibility(View.VISIBLE);
            Glide.with(context).load(list.get(position).getPhoto()).into(holder.msgImg);
        }
        String time = DeviceUtil.getDateTime(DeviceUtil.getDateFromMillis(Long.parseLong(list.get(position).getTimeline())));
        holder.msgTime.setText(time);

        return convertView;
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_system_msg_main.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.msg_time)
        TextView msgTime;
        @Bind(R.id.msg_tips)
        TextView msgTips;
        @Bind(R.id.msg_img)
        ImageView msgImg;
        @Bind(R.id.msg_title)
        TextView msgTitle;
        @Bind(R.id.msg_content)
        TextView msgContent;
        @Bind(R.id.msg_client)
        TextView msgClient;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
